package cn.tedu.home1ess.admin.navgation.service.Impl;

import cn.tedu.home1ess.admin.navgation.dao.repository.INavgationRepository;
import cn.tedu.home1ess.admin.navgation.pojo.param.NavgationAddParam;
import cn.tedu.home1ess.admin.navgation.pojo.param.NavgationUpdateParam;
import cn.tedu.home1ess.admin.navgation.pojo.po.NavgationPO;
import cn.tedu.home1ess.admin.navgation.pojo.vo.NavgationVO;
import cn.tedu.home1ess.admin.navgation.service.INavgationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class NavgationServiceImpl implements INavgationService {
    @Autowired
    private INavgationRepository navgationRepositoryImpl ;

    @Override
    public List<NavgationVO> listForContent() {
        //查找所有的内容
        List<NavgationPO> listForContent=navgationRepositoryImpl.selectContent();
        System.out.println(listForContent);
        //创建一个新的结果list
        List<NavgationVO> result=new ArrayList<>();
        //把po转换为我们要的结果,并根据类别id查找相应的类别
        for (NavgationPO po : listForContent) {
            NavgationVO navgationVO = new NavgationVO();
            BeanUtils.copyProperties(po,navgationVO);
            result.add(navgationVO);
        }



        return result;
    }
    //根据id查询
    @Override
    public NavgationVO selectById(Long id) {
        System.out.println("Serviceid111111:"+id);
        NavgationPO navgationPO =  navgationRepositoryImpl.selectById(id);
        log.error("navgationPO"+navgationPO);
        NavgationVO navgationVO=new NavgationVO();
        BeanUtils.copyProperties(navgationPO,navgationVO);
        log.error("navgationVO"+navgationVO);
        return navgationVO;
    }

    //删除
    @Override
    public void DeleteNavgation(Long id) {
        //先根据name判断库中是否已存在
        NavgationPO navgationPO = navgationRepositoryImpl.selectById(id);
        log.error("navgationPO"+navgationPO);
        if (navgationPO==null){
            throw new RuntimeException("未查询到该条数据，请重新操作");
        }
        navgationRepositoryImpl.deleteNavgation(id);


    }

    @Override
    public void UpdateNavgation(NavgationUpdateParam navgationUpdateParam) {
        //先根据name判断库中是否已存在
        NavgationPO navgationPO = navgationRepositoryImpl.selectById(navgationUpdateParam.getId());
        log.error("navgationPO"+navgationPO);
        if (navgationPO==null){
            throw new RuntimeException("未查询到该条数据，请重新操作");
        }
        NavgationPO navgationPO1=new NavgationPO();
        BeanUtils.copyProperties(navgationUpdateParam,navgationPO1);
        navgationPO1.setCreateTime(new Date());
        System.out.println("navgationPO1:"+navgationPO1);
        navgationRepositoryImpl.updateNavgation(navgationPO1);

    }
    //增加导航
    @Override
    public void addNavgation(NavgationAddParam navgationAddParam) {
           //先根据name判断库中是否已存在
        NavgationPO navgationPO = navgationRepositoryImpl.selectByName(navgationAddParam.getName());
        if (navgationPO!=null){
           throw new RuntimeException("已存在同名导航,请检查");
        }

        NavgationPO nPO=new NavgationPO();
        BeanUtils.copyProperties(navgationAddParam,nPO);
        //默认给他一个未启用的状态
        nPO.setIsStart(0);
        nPO.setCreateTime(new Date());
        navgationRepositoryImpl.save(nPO);


    }
}




































